<?php
class Hydrum_Model_Pathname
{
    /**
     * Pathname to model
     * 
     * @var string
     */
    protected $_pathname;

    /**
     * Parsed path name 
     * 
     * @var array
     */
    protected $_parts;

    const FORMAT_DASHED = 'dashed';
    const FORMAT_CAMELCASE = 'camelcase';

    public function __construct($pathname)
    {
        $this->_pathname = (string) $pathname;

        $this->parse();
    }

    protected function parse()
    {
        $parts = explode('/', $this->_pathname);

        if (count($parts) < 2) {

            throw new Exception('Wrong format for model pathname. ' .
                'It should be "[module-name]/[model-name]".');
        }

        $parts = array(
            'module' => $parts[0],
            'model'  => $parts[1]
        );

        foreach ($parts as $key => $value) {

            if (empty($value)) {

                throw new Exception(ucfirst($key) .
                    ' name should be specified.');
            }

            if (strpos($value, ' ') !== false) {

                throw new Exception('Space characters are allowed for ' .
                    $key . ' name.');
            }
        }

        $this->_parts = $parts;
    }

    protected function getFilters($format)
    {
        $filter = new Zend_Filter();

        switch ($format) {

        case self::FORMAT_CAMELCASE:
            $filter = new Zend_Filter_Word_DashToCamelCase;
            break;

        case self::FORMAT_DASHED:
        default:
            $filter
                ->addFilter(new Zend_Filter_Word_CamelCaseToDash)
                    ->addFilter(new Zend_Filter_StringToLower());
            break;
        }

        return $filter;
    }

    protected function formatName($name, $format)
    {
        $filter = $this->getFilters($format);

        $name = $filter->filter($name);

        return $name;
    }

    /**
     * Get module name with corresponding format
     * 
     * @param string $format
     * @return string
     */
    public function getModuleName($format = self::FORMAT_DASHED)
    {
        $name = $this->formatName($this->_parts['module'], $format);

        return $name;
    }

    /**
     * Get model name with corresponding format
     * 
     * @param string $format
     * @return string
     */
    public function getModelName($format = self::FORMAT_DASHED)
    {
        $name = $this->formatName($this->_parts['model'], $format);

        return $name;
    }

    /**
     * Get model class name with provided pathname
     * 
     * @return string
     */
    public function getModelClassName()
    {
        $inflection = 'Module_:module_Model_:model';
        
        $className = strtr($inflection, array(
            ':module' => $this->getModuleName(self::FORMAT_CAMELCASE),
            ':model'  => $this->getModelName(self::FORMAT_CAMELCASE)
        ));
        
        return $className;
    }

    public function __toString()
    {
        return $this->_pathname;
    }
}
